/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.aurelius.testngexecutionlogger;

import org.testng.*;

/**
 *
 * @author Lukasz
 */
public class EventListener2 implements IInvokedMethodListener , ISuiteListener{

    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        
    }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {

        process(testResult, method);
    }

    private void process(ITestResult testResult, IInvokedMethod method) {
        StringBuilder stringBuilder = new StringBuilder(50);
        stringBuilder.append(" ");

        int status = testResult.getStatus();
        if (ITestResult.FAILURE == status) {
            stringBuilder.append("[");
            stringBuilder.append("FAIL");
            stringBuilder.append("]");
        } else if(ITestResult.SUCCESS == status ) {
            stringBuilder.append("[");
            stringBuilder.append("PASS");
            stringBuilder.append("]");
        } else if(ITestResult.SKIP == status ) {
            stringBuilder.append("[");
            stringBuilder.append("SKIP");
            stringBuilder.append("]");
        }

//        stringBuilder.append(" Class: ");
        stringBuilder.append(" - ");
        stringBuilder.append(testResult.getTestClass().getRealClass().getSimpleName());
//        stringBuilder.append(" method name: ");
        stringBuilder.append(" - ");
        stringBuilder.append(method.getTestMethod().getMethodName());
        stringBuilder.append(" - ");
        stringBuilder.append(testResult.getEndMillis() - testResult.getStartMillis());

        TestResultLogger.log(stringBuilder.toString());
    }

    @Override
    public void onStart(ISuite suite) {
        TestResultLogger.log("Begin");
    }

    @Override
    public void onFinish(ISuite suite) {
        TestResultLogger.log("End");

    }
}
